
package com.thalesgroup.nl.trtdelft.dein.mapper.example;

import com.thalesgroup.nl.trtdelft.dein.mapper.AbstractMappedWebService;
import com.thalesgroup.nl.trtdelft.dein.servlet.impl.Client;
import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.apache.http.HttpException;

/**
 *
 * @author AbdelhaqA
 */

public class GraphDB extends AbstractMappedWebService {   
    @Override
    @SuppressWarnings("unchecked")
    public void input(String senderAddress, String inputVariableName, Serializable input) {
 
    }
         
    @Override
    public void start() {
         //   String userId = "10";
         //   String passWord = "TUCDE0001";
        // receive data from  SNIF
        String userId = (String) queryReceiveVariableContent.get("userId");
        String passWord = (String) queryReceiveVariableContent.get("passWord");
          
        JSONObject query = new JSONObject();
            
        // query.put("query", "match (n) where n.password='TUCDE0001' and n.id=10 return n.AccessTokenUNI, n.refresh_token, n.id_token");
        query.put("query", "match (n) where n.password='"+passWord+"' and n.id="+userId+" return n" );
        try {
        String result = Client.post("http://localhost:7474/db/data/cypher/", query.toJSONString());              
        // Object parse = JSON.parse(result);
        if ( result.contains("TUCDE0001") )
            // send data to SNIF
            outputs.put("loginStatus", "True");
        else                    
        //   Level parse = parse(result);                  
            outputs.put("loginStatus", "False");
        } catch (URISyntaxException ex) {
            Logger.getLogger(GraphDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HttpException ex) {
            Logger.getLogger(GraphDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GraphDB.class.getName()).log(Level.SEVERE, null, ex);
        }
      //  sendMapperInformationInterface.sendOutput();
    }        
    @Override
    public void computeOutput(String variableName) throws IllegalStateException {
        computeOutput();
    }
    @Override
    public Serializable getCurrentOutput(String variableName) throws IllegalStateException {
        return outputs.get(variableName);
    }

}

